﻿using System;
using System.Collections.Generic;
using System.Text;

using Semantics;
using Semantics.Base;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;

namespace Semantics.Example.ParserExample
{
    class dbSql
    {
        public static SqlConnection ConnectDB()
        {
            
            string str = "server=localhost\\SQLExpress;Initial CataLog=IntelliAgent;Integrated Security=SSPI";
            SqlConnection conn = new SqlConnection(str);
            return conn;
        }

        public DataSet GetAll(string tableName)
        {
            SqlConnection conn = ConnectDB();
            conn.Open();
            string cmd = "select * from " + tableName;
            SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, tableName);
            return ds;
        }

        public bool  AddSemanticTree(string Sentence,string SemanticTree)
        {
            bool result=false;
            string cmd;
            SqlConnection conn = ConnectDB();
            conn.Open();
            string find = "select * from Sentence where Sentence='" + Sentence+"'" ;
            SqlCommand sqlcmd = new SqlCommand(find, conn);
            //如果数据库中有该Sentence
            int temp = sqlcmd.ExecuteNonQuery();
            SqlDataReader dr = sqlcmd.ExecuteReader();

            if (dr.Read())
            {
                
                //string cmd="insert Sentence(Sentence) values('美国队的内线中有很多实力强大的球员。')";
                cmd = "update Sentence set SemanticTree='" + SemanticTree + "' where Sentence='" + Sentence + "'";

            }
            else
            {
                cmd = "insert Sentence(Sentence,SemanticTree) values ('" + Sentence + "','" + SemanticTree + "')";
            }
            dr.Close();
            sqlcmd = new SqlCommand(cmd, conn);
            if (sqlcmd.ExecuteNonQuery() >= 1)
                result = true;
            conn.Close();
            return result;
        }
    }
}
